//
//  CTopBarView.m
//  HCHK_iphone
//
//  Created by yilong zhang on 12-1-13.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CTopBarView.h"

@implementation CTobBarItem

@synthesize m_Bg;
@synthesize m_Btn;
@synthesize m_Target;
@synthesize m_Action;

-(id)initWithFrame:(CGRect)frame Image:(UIImage*)image ImSize:(CGSize)imsize
{
    self = [super initWithFrame:frame];
    if (self)
    {        
        m_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [m_Btn setBackgroundImage:image forState:UIControlStateNormal];
        m_Btn.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        [m_Btn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:m_Btn];
    }
    
    return self;
}

-(void)AddTarget:(id)target Action:(SEL)action
{
    m_Target = target;
    m_Action = action;
}

-(void)OnButtonDown:(UIButton*)sender
{
    [m_Target performSelector:m_Action withObject:self];
}

@end

//===============================================================================================================================

@implementation CTopBarView

@synthesize delegate;
//@synthesize m_RetBtn;
//@synthesize m_Log;
//@synthesize m_Title;
//@synthesize m_OptBtn;
//@synthesize m_HomeBtn;
//@synthesize m_Search;
//@synthesize m_Bg;

- (id)initWithFrame:(CGRect)frame
{
    //CGRect rect = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, 44);
    self = [super initWithFrame:frame];
    if (self) 
    {
        //CGRect rect = frame;
        
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        
        UIImageView *m_Bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        m_Bg.image = [UIImage imageNamed:@"barbg.png"];
        [self addSubview:m_Bg];
        [m_Bg release];
        
        /*m_Search = [[CTobBarItem alloc] initWithFrame:CGRectMake(10, 3, 30, 30) Image:[UIImage imageNamed:@"search.png"] ImSize:CGSizeMake(30, 30)];
        m_Search.tag = 3;
        [m_Search AddTarget:self Action:@selector(OnTopBarDown:)];
        [self addSubview:m_Search];
        [m_Search release];
        
        m_Log = [[UILabel alloc] initWithFrame:CGRectMake(17, 0, 100, rect.size.height)];
        m_Log.text = @"好吃客";
        m_Log.textColor = [UIColor whiteColor];
        m_Log.font = [UIFont fontWithName:@"Helvetica" size:21];
        m_Log.backgroundColor = [UIColor clearColor];
        [self addSubview:m_Log];
        [m_Log release];
        
        m_RetBtn = [[CTobBarItem alloc] initWithFrame:CGRectMake(10, 9, 30, 30) Image:[UIImage imageNamed:@"return.png"] ImSize:CGSizeMake(30, 30)];
        m_RetBtn.tag = 0;
        [m_RetBtn AddTarget:self Action:@selector(OnTopBarDown:)];
        [self addSubview:m_RetBtn];
        [m_RetBtn release];
        
        m_Title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, rect.size.height)];
        m_Title.center = CGPointMake(rect.size.width/2, rect.size.height/2);
        m_Title.backgroundColor = [UIColor clearColor];
        m_Title.textColor = [UIColor whiteColor];
        m_Title.textAlignment = UITextAlignmentCenter;
        [self addSubview:m_Title];
        [m_Title release];
        
        m_OptBtn = [[CTobBarItem alloc] initWithFrame:CGRectMake(self.frame.size.width-50, 4, 40, 28) Image:[UIImage imageNamed:@"option.png"] ImSize:CGSizeMake(30, 28)];
        m_OptBtn.tag = 1;
        [m_OptBtn AddTarget:self Action:@selector(OnTopBarDown:)];
        [self addSubview:m_OptBtn];
        [m_OptBtn release];
        
        m_HomeBtn = [[CTobBarItem alloc] initWithFrame:CGRectMake(m_OptBtn.frame.origin.x-39, 4, 40, 27) Image:[UIImage imageNamed:@"local.png"] ImSize:CGSizeMake(30, 27)];
        m_HomeBtn.tag = 2;
        [m_HomeBtn AddTarget:self Action:@selector(OnTopBarDown:)];
        [self addSubview:m_HomeBtn];
        [m_HomeBtn release];*/
    }
    
    return self;
}

-(void)AddItem:(UIButton*)button
{
    [self addSubview:button];
    [button addTarget:self action:@selector(OnTopBarDown:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)OnTopBarDown:(UIButton*)sender
{
    [delegate DidTopBarBtnsClicked:sender];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
